<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<%@include file="static/common/common.jspf"%>
</head>
<body>
<div id="userToolbar" style="padding:5px;height:auto;">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" id="addButton" onclick="openUserWin(1)">新增用户</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" id="updateButton" onclick="openUserWin(2)">修改用户</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" id="deleteButton" onclick="batchDeleteUser()">删除用户</a> 
</div>
<table id="userGrid" class="easyui-datagrid" title="用户信息列表" style="width:700px;height:250px;"
					data-options="singleSelect:false,
							collapsible:true,
							url:'user!selectUserPageData',
							method:'get',
							fit:true,
							toolbar:'#userToolbar',
							pagination:true,
							pageSize:5,
							pageList:[3,4,5,10],
							striped:true
					">
			<thead>
				<tr>
					<th data-options="field:'ck',checkbox:true"></th>
					<th data-options="field:'id',width:80,hidden:'hidden'">用户ID</th>
					<th data-options="field:'username',width:100">用户名</th>
					<th data-options="field:'password',width:80,align:'right'">密码</th>
					<th data-options="field:'roleid',width:80,hidden:'hidden'">角色ID</th>
				</tr>
			</thead>
	</table>
	<!-- 新增或修改 -->
 <div id="userDialog"  class="easyui-dialog" style="width:400px;height:0px;"
	data-options="iconCls:'icon-edit',resizable:true,modal:true,closed:true">
	<form id="userForm" method="get">
		<div style="margin-left:20px;margin-bottom:20px;">
			<input id="id" type="hidden" name="user.id">
	        用户名：<input id="name" class="easyui-textbox" name="user.username" style="width:80%;height:40px;padding:12px;" 
        			data-options="prompt:'必填',required:true">
		</div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
	        密码：<input id="password" class="easyui-textbox" name="user.password" type="password" style="width:80%;height:40px;padding:12px;" 
        			data-options="prompt:'至少长度为3',required:true">
		</div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
        	<select id="roleid" class="easyui-combobox" name="user.roleid" style="width:80%;height:40px;padding:12px;"
        			data-options="valueField:'roleId',editable:false,textField:'roleName',url:'user!selectAllRoleList',label:'角色名:'">
        	</select>
        </div>		
        
        <div style="margin-left:20px;margin-bottom:20px;">
        	<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="padding:5px 0px;width:30%;" onclick="submitForm()" >
        	<span style="font-size:14px;">提交</span>
        	</a>
        </div>
	</form>
</div>	
<script type="text/javascript">
	//	$("#addButton").hide();
	var addUrl;
	var updateUrl;
	var deleteUrl;
	$(function(){
		var mIDStr=2;
		$("#addButton").hide();
		$("#updateButton").hide();
		$("#deleteButton").hide();
		$.ajax({
			url:"user!buttonControl",
			type:"post",
			data:{
				menui:mIDStr
			},
			dataType:"json",
			success:function(data){
				$.each(data,function(index,item){
					if(item.menuName =='新增'){
						addUrl=item.menuRole;
						$("#addButton").show();
					}
					
					if(item.menuName=='编辑'){
						updateUrl=item.menuRole;
						$("#updateButton").show();
					}
					
					if(item.menuName=='删除'){
						deleteUrl=item.menuRole;
						$("#deleteButton").show();
					}
				});
			}
		})
		
	})
	

var url;
function openUserWin(flag){
	$("#userForm").form("reset");
	//如果flag为1的时候则为新增,直接打开窗口
	//如果flag为2的时候则为修改,将选中的行数据赋给对应表单元素
	if(flag !=1){
		var rows=$("#userGrid").datagrid("getChecked");
		if(rows.length !=1){
			winAlert("提示","请选择一行数据进行修改！");
		}else{
			//"user!editUser"
			url=updateUrl; 
			var data=rows[0];
			
			$("#id").val(data.id);
			$("#name").textbox('setValue',data.username);
			$("#password").textbox('setValue',data.password);
			$("#roleid").combobox('setValue',data.roleid);
		}
	}else{
		//'user!addUser'
		url =addUrl;
	}
	$("#userDialog").dialog('open');
} 

function submitForm(){
	$('#userForm').form('submit', {
		url: url,
		success: function(result){
			var data = JSON.parse(result);
			if(data.isok == true){
				$("#userGrid").datagrid('reload');
				winAlert("成功!", "操作成功!")
			}else{
				winAlert("错误!", "操作失败!")
			}
			$("#userDialog").dialog('close');
		}
	});
}

	function batchDeleteUser(){
		//获取所有选中的行
		var rows=$("#userGrid").datagrid("getChecked");
		if(rows.length !=0){
			var ids;
			$.each(rows,function(index,item){
				if(index==0){
					ids=item.id;
				}else{
					ids=ids+","+item.id
				}
			});
			//'user!batchDeleteUser'
			 $.post(deleteUrl,{'ids':ids}, function(data){
				if(data.isok ==true){
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除成功！");
					}else{
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除失败！");
					}
			},'json'); 
			
			/* $.ajax({
				type:"post",
				url:"user!batchDeleteUser",
				data:{
					ids:ids
				},
				dataType:"json",
				traditional:true,
				success:function(data){
					if(data.isok ==true){
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除成功！");
					}else{
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除失败！");
					}
				}
			}) */
		}else{
			winAlert("提示","请至少选中一行进行删除！");
		}
	}
	
	function winAlert(title,msg){
		$.messager.show({
			title:title,
			msg:msg,
			showType:'fade',
			style:{
				right:'',
				bottom:''
			}
		})
	}
</script>
</body>
</html>